package com.prs.crm.action.oa.schedule;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.schedule.WorkPlan;
import com.prs.crm.domain.oa.schedule.WorkPlanReply;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.oa.schedule.WorkPlanReplyService;
import com.prs.crm.service.oa.schedule.WorkPlanService;

public class WorkPlanAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private WorkPlan workPlan;
	private WorkPlanService workPlanService;
	private WorkPlanReply workPlanReply;
	private WorkPlanReplyService workPlanReplyService;
	private List<WorkPlanReply> reply;
	
	private String title;
	
	private File upload;
	private String uploadFileName;
	
	private PersonService personService;
	public PersonService getPersonService() {
		return personService;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public WorkPlan getWorkPlan() {
		return workPlan;
	}
	public void setWorkPlan(WorkPlan workPlan) {
		this.workPlan = workPlan;
	}
	public WorkPlanService getWorkPlanService() {
		return workPlanService;
	}
	public void setWorkPlanService(WorkPlanService workPlanService) {
		this.workPlanService = workPlanService;
	}
	public WorkPlanReply getWorkPlanReply() {
		return workPlanReply;
	}
	public void setWorkPlanReply(WorkPlanReply workPlanReply) {
		this.workPlanReply = workPlanReply;
	}
	public WorkPlanReplyService getWorkPlanReplyService() {
		return workPlanReplyService;
	}
	public void setWorkPlanReplyService(WorkPlanReplyService workPlanReplyService) {
		this.workPlanReplyService = workPlanReplyService;
	}
	public List<WorkPlanReply> getReply() {
		return reply;
	}
	public void setReply(List<WorkPlanReply> reply) {
		this.reply = reply;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	@Action("listWorkPlan")
	public String listWorkPlan(){
		Person curPerson=AppContext.getAccountContext().currentUser();
		List<WorkPlan> wl=this.getWorkPlanService().getAllWorkPlan();
		for (WorkPlan ob : wl) {
			Person per=this.getPersonService().getPersonById(curPerson.getId());
			if (per!=null) {
				if (per.getSubordinateId()==null ||"".equals(per.getSubordinateId().trim())) {
					if (ob.getRecorder().equals(curPerson)) {
						ob.setShowState(curPerson.getId()+":1");
						this.getWorkPlanService().submit(ob);
					}
				}else{
					String[] pers=per.getSubordinateId().split(",");
					for (String p : pers) {
						if (ob.getRecorder().getId().equals(Integer.parseInt(p))) {
							ob.setShowState(curPerson.getId()+":1");
							this.getWorkPlanService().submit(ob);
						}
					}
				}	
			}
		}
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(WorkPlan.class);
		criteria.add(Restrictions.eq("showState", curPerson.getId()+":1"));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getWorkPlanService().listByPage(criteria,pagination));
		return SUCCESS;
	}
	
	@Action("editWorkPlan")
	public String editWorkPlan(){
		return SUCCESS;
	}
	
	@Action("viewWorkPlanReply")
	public String viewWorkPlanReply(){
		this.reply=this.workPlan.getWorkPlanReply();
		this.title=this.workPlan.getTitle();
		workPlan.setReadyNum(workPlan.getReadyNum()+1);
		this.getWorkPlanService().submit(workPlan);
		return SUCCESS;
	}
	
	@Action("editWorkPlanReply")
	public String editWorkPlanReply(){
		this.title=this.workPlan.getTitle();
		this.reply=this.getWorkPlanReplyService().getAllWorkPlanReply();
		return SUCCESS;
	}
	@Action(value = "saveWorkPlan", results = {
			@Result(name = "success", type = "redirect", location = "viewWorkPlanReply?workPlan=${workPlan.getId()}"),
			@Result(name = "error", type = "chain", location = "editWorkPlan?workPlan=${workPlan.getId()}") })
	public String saveWorkPlan(){
		if (upload!=null) {
			this.workPlan.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		workPlan.setLastTime(sf.format(date));
		if(workPlan.getId()!=null){
			Person person=new Person();
			person=AppContext.getAccountContext().currentUser();
			workPlan.setUpdatePerson(person.getResume().getName());
			workPlan.setType(1);
			workPlan.setUpdateTime(sf.format(date));
			this.getWorkPlanService().submit(workPlan);
		}else{
			workPlan.setReadyNum(0);
			workPlan.setReplyNum(0);
			workPlan.setType(0);
			this.getWorkPlanService().save(workPlan);
		}
		return SUCCESS;
	}
	@Action(value = "saveWorkPlanReply", results = {
			@Result(name = "success", type = "redirect", location = "viewWorkPlanReply?workPlan=${workPlan.getId()}"),
			@Result(name = "error", type = "chain", location = "editWorkPlanReply?workPlan=${workPlan.getId()}") })
	public String saveWorkPlanReply(){
		if (upload!=null) {
			this.workPlanReply.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		workPlan.setReplyNum(workPlan.getReplyNum()+1);
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		workPlan.setLastTime(sf.format(date));
		this.getWorkPlanService().submit(workPlan);
		workPlanReply.setWorkPlan(workPlan);
		this.workPlanReply.setTitle(title);
		this.getWorkPlanReplyService().save(workPlanReply);
		return SUCCESS;
	}
	@Action(value = "removeWorkPlan", results = {
			@Result(name = "success", type = "redirect", location = "listWorkPlan"),
			@Result(name = "error", type = "chain", location = "listWorkPlan") })
	public String removeWorkPlan(){
		if (workPlanReply!=null) {
			this.getWorkPlanReplyService().remove(workPlanReply);
		}
		this.getWorkPlanService().remove(workPlan);
		return SUCCESS;
	}
	@Action(value = "removeWorkPlanReply", results = {
			@Result(name = "success", type = "redirect", location = "listWorkPlan"),
			@Result(name = "error", type = "chain", location = "listWorkPlan") })
	public String removeWorkPlanReply(){
		this.getWorkPlanReplyService().remove(workPlanReply);
		workPlanReply.setWorkPlan(workPlanReply.getWorkPlan());
		workPlanReply.getWorkPlan().setReplyNum(workPlanReply.getWorkPlan().getReplyNum()-1);
		this.getWorkPlanService().submit(workPlanReply.getWorkPlan());
		return SUCCESS;
	}
}
